package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.admin.MarketAdminService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {
    @Autowired
    private MarketAdminService marketAdminService;

    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"管理员", "管理员列表"}, button = "列表")
    @GetMapping("list")
    public Object list(@RequestParam(value = "page", required = false) Integer page,
                      @RequestParam(value = "limit", required = false) Integer limit,
                      @RequestParam(value = "username", required = false) String username,
                      @RequestParam(value = "sort", required = false) String sort,
                      @RequestParam(value = "order", required = false) String order){
        List<MarketAdmin> marketAdminList = marketAdminService.list(limit, page, username, sort, order);
        return ResponseUtil.okList(marketAdminList);

    }
    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = {"管理员", "管理员删除"}, button = "删除")
    @PostMapping("delete")
    public Object delete(@RequestBody Map<String, Object> requestBody, HttpServletResponse resp) throws IOException {
        Integer id = (Integer) requestBody.get("id");
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(id);
        MarketAdmin delete = marketAdminService.delete(marketAdmin);
        if(delete!=null) {
            return ResponseUtil.ok(marketAdmin);
        }else return ResponseUtil.badArgument();
    }

    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"管理员", "管理员创建"}, button = "添加")
    @PostMapping("create")
    public Object create(@RequestBody Map<String, Object> requestBody) {
        String username = (String) requestBody.get("username");
        String password = (String) requestBody.get("password");
        String avatar = (String) requestBody.get("avatar");
        List<Integer> roleIds = (ArrayList<Integer>) requestBody.get("roleIds");
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setUsername(username);
        marketAdmin.setPassword(password);
        marketAdmin.setAvatar(avatar);
        marketAdmin.setRoleIds(roleIds.toArray(new Integer[0]));
        marketAdmin.setAddTime(LocalDateTime.now());
        marketAdmin.setUpdateTime(LocalDateTime.now());
        marketAdminService.create(marketAdmin);
        return ResponseUtil.ok(marketAdmin);
    }

    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"管理员", "管理员更新"}, button = "修改")
    @PostMapping("update")
    public Object update(@RequestBody Map<String, Object> requestBody) {
        Integer id = (Integer) requestBody.get("id");
        String username = (String) requestBody.get("username");
        String password = (String) requestBody.get("password");
        String avatar = (String) requestBody.get("avatar");
        List<Integer> roleIds = (ArrayList<Integer>) requestBody.get("roleIds");
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(id);
        marketAdmin.setUsername(username);
        marketAdmin.setPassword(password);
        marketAdmin.setAvatar(avatar);
        marketAdmin.setRoleIds(roleIds.toArray(new Integer[0]));
        marketAdmin.setUpdateTime(LocalDateTime.now());
        marketAdminService.update(marketAdmin);
        return ResponseUtil.ok(marketAdmin);
    }
}
